package com.block.api.controller.sys;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.SessionBean;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.constant.RedisKeyConstant;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelBaseInfo;
import com.block.match.service.IDataMatchOrderItemService;
import com.block.match.service.IMatchApiBHandlerService;
import com.block.sms.service.ISmsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("api/home")
@Validated
public class HomeController {

    @Autowired
    ISmsRecordService smsRecordService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    IDataMatchOrderItemService dataMatchOrderItemService;
    @Autowired
    IMatchApiBHandlerService matchApiBHandlerService;

    /**
     * 主页信息
     *
     */
    @NeedToken(need = false)
    @RequestMapping(value = "/mainInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String mainInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                              @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                                @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                                @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                @RequestAttribute(value = "uri") String uri,
                                @RequestAttribute(value = "startDate") Date startdate,
                                @RequestAttribute(value = "ip") String ip) throws ApiException {

        JSONObject jsonObject = new JSONObject();

//        String redisKeyToLogin = RedisKeyConstant.MEMBER_SOURCE_SIGN_FLAG;
//        String ToLogin = stringRedisTemplate.opsForValue().get(redisKeyToLogin);
        jsonObject.put("toLoan", "2");//0 敬请期待 1 跳产品 2 重复授权
//        if (StrUtil.isEmpty(ToLogin)){
//            jsonObject.put("toLoan", "0");
//        }else{
//            jsonObject.put("signFlag", "1");
//        }

        jsonObject.put("randomUuid", RandomUtil.randomString(32));//

        // 是否进行签名
        String redisKey = RedisKeyConstant.MEMBER_SOURCE_SIGN_FLAG;
        String signFlag = stringRedisTemplate.opsForValue().get(redisKey);
        if (StrUtil.isEmpty(signFlag)){
            jsonObject.put("signFlag", "0");
        }else{
            jsonObject.put("signFlag", "1");
        }

        // 查询用户是否有推送机构信息
        JSONObject pushProductInfo = dataMatchOrderItemService.getLastedSuccessPushProduct(sessionBean.getUid());
        jsonObject.put("pushProductInfo", pushProductInfo);

        if (!ObjectUtil.equals(sessionBean.getUid(), 0L)){
            matchApiBHandlerService.getApiBMatchResult(sessionBean.getUid());
        }

        ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
    }


    /**
     * 主页信息
     *
     */
    @NeedToken
    @RequestMapping(value = "/successOrder", method = {RequestMethod.POST, RequestMethod.GET})
    public String successOrder(@RequestAttribute(value = "tranData") JSONObject tranData,
                           @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                           @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                           @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                           @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                           @RequestAttribute(value = "uri") String uri,
                           @RequestAttribute(value = "startDate") Date startdate,
                           @RequestAttribute(value = "ip") String ip) throws ApiException {

        JSONObject jsonObject = new JSONObject();

        // 查询用户是否有推送机构信息
        JSONObject pushProductInfo = dataMatchOrderItemService.getLastedSuccessPushProduct(sessionBean.getUid());
        jsonObject.put("pushProductInfo", pushProductInfo);

        ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, startdate, uri);
    }

}
